Interaction and setup of multiple Wiimotes

This more complex buzzer application creates a game show type of application and shows interactivity between up to 4 Wiimotes, a mouse, and the application.

Here is the completed application buzzerMoteMultimote_app.fla with comments, and below are just the comments and code in the application.

Frame 1:

//stop on frame one until the user has made a choice about Wiimotes.
stop()
 
//The user at the start of the application is prompted to choose how many Wiimotes are connected.
//WiiFlash can't do this on its own you have to set it.
var numberOfWiimotes:Number
 
 
// I use a loop to attach the same event listeners to multiple movie clips
//Initially there are 4 buttons on the screen where the user needs to select how many Wiimotes are connected.
//There are 4 buttons so the loop is set to run 4 times
 
for(var wiis:int = 1; wiis<=4; wiis++){
 
    //this["wii_"+wiis+"_mc"] refers to buttons called wii_1_mc, wii_2_mc, wii_3_mc and wii_4_mc.
 
    //when a button is clicked it calls the setWiiMoteCount function
    this["wii_"+wiis+"_mc"].addEventListener(MouseEvent.MOUSE_UP, setWiiMoteCount)
    //the number of the movieclip is stored inside a variable called moteAmount to use later
    this["wii_"+wiis+"_mc"].moteAmount = wiis
    //the movieclip is given a buttonmode of true to make it act like a button by showing the pointer... perhaps I should just have used a button?
    this["wii_"+wiis+"_mc"].buttonMode = true
}
 
function setWiiMoteCount(event:MouseEvent):void{
    //numberOfWiimotes is set to the current buttons moteAmount - ie the button number
    numberOfWiimotes = event.currentTarget.moteAmount
    //lastly the application is sent to frame two
    gotoAndStop(2)
}
 
 

Frame 2:


//import the required classes this is the same for all applications that use the WiiFlash Server
//no changes should be, or need to be, made here.
import org.wiiflash.Wiimote;
import org.wiiflash.events.ButtonEvent;
import org.wiiflash.events.WiimoteEvent;
import flash.events.*;
 
 
//This sets up the new Wiimote connection by using the value stored in the numberOfWiimotes variable set
//by clicking one of the buttons in frame 1.
for(var i:int = 0; i< numberOfWiimotes; i++){
 
    //using the looping method to make multiple Wiimote connections does not mean that the Wiimotes need different names
    //below is fine, Flash knows that they are individual Wiimote connections (or objects)
 
    //make a new Wiimote connection
    var myWiimote:Wiimote = new Wiimote();
    //connect to the Wiimote
    myWiimote.connect ();
    //Call the onWiimoteConnect funciton when each time a Wiimote connects
    myWiimote.addEventListener( Event.CONNECT, onWiimoteConnect );
}
 
function onWiimoteConnect ( pEvent:Event ):void
 
{
    //Each individual Wiimote (pEvt.target) is assisgned the same events
        pEvent.target.addEventListener( ButtonEvent.A_PRESS, onAPressed );
        pEvent.target.addEventListener( ButtonEvent.A_RELEASE, onAReleased);
        pEvent.target.addEventListener( ButtonEvent.PLUS_PRESS, onPlusPressed);
        pEvent.target.addEventListener( ButtonEvent.MINUS_PRESS, onMinusPressed);
}
 
// As there is (or meant to be) more than one Wiimote connection we need to disable other Wiimotes while the first
//button is pressed. The two variables below are used to do that, more explanations are inside particular funcitons.
var buttonsEnabled:Boolean = true
var buttonPressed:Number
 
function onAPressed( pEvt:ButtonEvent ):void
{
    //if the buttonsEnabled is equal to true the run the code that creates the buzzer effect.
    if(buttonsEnabled){
        //pEvt.currentTarget.id is the NUMBER of the Wiimote that called (or triggered) this function.
        //if pEvt.currentTarget.id is equal to 1  then the code below essentially says:
        //buzzer_2_mc.gotoAndStop(2)  -- buzzer 2 because I am adding one to the id number so that it matches my buzzer names.
        this["buzzer"+(pEvt.currentTarget.id+1)+"_mc"].gotoAndStop(2);
 
        //vibrate the remote when pressed for 500 milliseconds or 1/2 a second
        pEvt.currentTarget.rumbleTimeout = 500;
 
        //set the buttons enabled variable to false so that no other Wiimotes can get 'buzz' while the first one is still pressed
        buttonsEnabled=false
 
        //buttonPressed is set to the current Wiimote with the A button still held down.
        buttonPressed = pEvt.currentTarget.id
    }
}
 
function onAReleased ( pEvt:ButtonEvent ):void
 
{
    //pEvt.currentTarget.id is the NUMBER of the Wiimote that called (or triggered) this function.
    //if pEvt.currentTarget.id is equal to 1  then the code below essentially says:
    //buzzer_2_mc.gotoAndStop(1)  -- buzzer 2 because I am adding one to the id number so that it matches my buzzer names.
    //All Wiimotes can call this line of code at any time as it is only resetting their personal buzzer to its normal unbuzzing state.
    this["buzzer"+(pEvt.currentTarget.id+1)+"_mc"].gotoAndStop(1);
 
    //if the buttonPressed (buttonPressed variable that was written to in the previous funciton) is equal to the id number
    //of the Wiimote that calls this function can reset the buttonsEnable variable and let the other Wiimotes work again.
 
    //ie the Wiimote that was pressed first is the only one that can make the appropriate match.
    if(buttonPressed == pEvt.currentTarget.id){
        buttonsEnabled=true
    }
}
 
function onPlusPressed ( pEvt:ButtonEvent ):void
{
    //This is here to demonstrate how to a Wiimote can take control of the mouse.
    //The Wiimote can now act as a pointer, pressing the a button acts like a mouse click. (it will probably also still make the buzzer work...)
    //You will need a sensor bar for this to work.
    pEvt.currentTarget.mouseControl = true
 
}
function onMinusPressed ( pEvt:ButtonEvent ):void
 
{
    //This removes the mouseControl.
    //This is really important as if mouseControl isn't removed before the application is quit the Wiimote will still
    //be enabled and you won't be able to effectively use the mouse
    pEvt.currentTarget.mouseControl = false
}
 
 
 
//So there are up to 4 Wiimotes working but a mouse can still work also.
 
//Below some code is written to enable scoring, clicking on the upper half of one of the coloured rectangular
//boxes of the 'podiums' will increase the score, clicking on the lowere half will decrease it.
//Perhaps a teacher could do the scoring.
 
score_1_mc.addEventListener(MouseEvent.MOUSE_UP, doScore)
score_1_mc.textBox = score_1_txt
score_2_mc.addEventListener(MouseEvent.MOUSE_UP, doScore)
score_2_mc.textBox = score_2_txt
score_3_mc.addEventListener(MouseEvent.MOUSE_UP, doScore)
score_3_mc.textBox = score_3_txt
score_4_mc.addEventListener(MouseEvent.MOUSE_UP, doScore)
score_4_mc.textBox = score_4_txt
 
 
function doScore(event:MouseEvent):void{
    var obj:Object = event.currentTarget
    if(obj.mouseY < 0){
        obj.textBox.text = Number(obj.textBox.text) + 25
    }else{
        obj.textBox.text = Number(obj.textBox.text) - 25
    }
}